package com.example.yiwei.crm.view.activity.jdjycooractivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.entity.MessageEntity;
import com.example.yiwei.crm.json.MessageJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.MessageAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class NewWorkActivity extends AppCompatActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{

    private ImageView img_back;
    private AutoListView listView;
    private SharedPreferences sp;
    private List<MessageEntity> list;
    private MessageAdapter adapter;
    private List<MessageEntity> alist;
    private ImageView img_search,img_luru,img_me,img_tj;
    private int a=1;
    private int page = 1;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<MessageEntity> result = (List<MessageEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    listView.onRefreshComplete();
                    if (list!=null){
                        list.clear();
                    }
                    if (list!=null){
                        list.addAll(result);
                    }else {
                        list = result;
                    }
                    if (a==1){
                        adapter = new MessageAdapter(NewWorkActivity.this,list);
                        listView.setAdapter(adapter);
                        a++;
                    }
                    break;
                case AutoListView.LOAD:
                    listView.onLoadComplete();
                    if (result!=null){
                        list.addAll(result);
                    }

                    break;
            }
            if (result!=null) {
//                listView.setResultSize(result.size());
            }else{
//                listView.setResultSize(0);
            }
            adapter.notifyDataSetChanged();

        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_work);
        img_back = (ImageView) findViewById(R.id.detailed_doc_img_back);
        listView = (AutoListView) findViewById(R.id.simple_health_education_listview);
        img_search= (ImageView) findViewById(R.id.item_image_cx);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        listView.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        listView.setDividerHeight(1);
        listView.setOnRefreshListener(this);
        listView.setOnLoadListener(this);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                String json = object.toString();
                String result = NetWorkUtils.getCount(baseClass.new_work_message_URL+1,json);
                Log.d("aa",result);
                list= MessageJson.getmessage(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (list!=null){
                            adapter = new MessageAdapter(NewWorkActivity.this,list);
                            listView.setAdapter(adapter);
                        }
                    }
                });
            }
        }).start();
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NewWorkActivity.this,JDJYCoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();


            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NewWorkActivity.this,JDJYCoorActivity.class);
                intent.putExtra("lr","lr");
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NewWorkActivity.this,JDJYCoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NewWorkActivity.this,JDJYCoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject object = new JSONObject();
                        try {
                            object.put("addoredit","edit");
                            object.put("XXB01",list.get(position-1).getID());
                            object.put("XXB03",1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        final String result = NetWorkUtils.getCount(baseClass.edit_work_message_URL,json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject object1 = new JSONObject(result);
                                    boolean b = object1.getBoolean("ifsuccess");
                                    if (b){
                                        onRefresh();
                                    }else {
                                        Toast.makeText(NewWorkActivity.this, "标记已读失败", Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }).start();
            }
        });
    }

    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }



    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            page=page+1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.new_work_message_URL+page,json);
            Log.d("aa",result);
            alist= MessageJson.getmessage(result);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                msg.obj = alist;
                handler.sendMessage(msg);
                Toast.makeText(NewWorkActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            page =1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.new_work_message_URL+1,json);
            Log.d("aa",result);
            alist= MessageJson.getmessage(result);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                listView.setVisibility(View.VISIBLE);
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                listView.setVisibility(View.GONE);
                Toast.makeText(NewWorkActivity.this, "无数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
            UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
            LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
}
